package com.gla.service;

import com.github.pagehelper.PageHelper;
import com.gla.commom.Result;
import com.gla.dao.OrderMapper;
import com.gla.dto.OrderDetailDTO;
import com.gla.dto.OrderInfoDTO;
import com.gla.dto.OrderVO;
import com.gla.entity.Order;
import com.gla.entity.OrderDetail;
import com.gla.enumeration.OrderStateEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class OrderService {

    @Resource
    OrderMapper orderMapper;

    @Resource
    CommodityService commodityService;

    @Autowired
    WalletService walletService;

    public List<Order> page(OrderVO orderVO) {
        PageHelper.startPage(orderVO.getPage(), orderVO.getSize());
        List<Order> list = orderMapper.select(orderVO.getOrder());
        return list;
    }

    /**
     * 获取订单详情
     * @param id
     * @return OrderInfoDTO
     */
    public OrderInfoDTO getDetail(Integer id) {
        OrderInfoDTO orderInfoDTO = orderMapper.getDetail(id);
        //获取订单内商品列表
        List<OrderDetailDTO> orderItemList= orderMapper.getOrderDetails(orderInfoDTO.getId());
        orderInfoDTO.setOrderDetailList(orderItemList);
        return orderInfoDTO;
    }

    public void update(Order order) {
        //原Order
        Order theOrder = orderMapper.selectById(order.getId()).get(0);
        //修改了state
        if(!theOrder.getState().equals(order.getState())) {
            //退款中->退款
            if(order.getState() == Order.REFUNDED) {
                List<OrderDetailDTO> commodityList = orderMapper.getOrderDetails(order.getId());
                double totalMoney = 0;
                for(OrderDetailDTO item : commodityList){
                    //修改库存
                    commodityService.updateNumber(item.getCommodity().getId(),
                            item.getCommodity().getNumber() + item.getNumber());
                    System.out.println("商品Id:"+item.getCommodity().getId()+"数量变为:"+item.getCommodity().getNumber() + item.getNumber());
                    //计算钱
                    totalMoney += item.getNumber() * item.getCommodity().getPrice();
                }
                //加钱
                walletService.update(order.getUser_id(), totalMoney);
            }
        }
        orderMapper.update(order);
    }

    public void delete(Integer id) {
        try{
            orderMapper.deleteOrderDetailByOrderId(id);
            orderMapper.deleteOrder(id);
        } catch (Exception e) {
            System.out.println("*******ERROR********\n删除订单出现错误");
            //回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

    public List<Order> getPendingOrder(Integer state) {
        Order order = new Order();
        order.setState(state);
        List<Order> list = orderMapper.select(order);
        return list;
    }
}
